package page.sample.AdministratorPage;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import page.sample.AuthorizationPage;
import dao.impl.ClientDao;
import entity.Client;
import entity.InfoUser;

public class ListClientsPage extends WebPage {

	public ListClientsPage(final InfoUser infoUser) {

		final ClientDao clientDao = new ClientDao(
				app.MyFirstWebApplication.DATA_XML_FOLDER);

		// get list of Circle objects using DAO
		final List<Client> allClients = clientDao.getAll();

		add(new DataView<Client>("table-rows", new ListDataProvider<Client>(
				allClients)) {
			@Override
			protected void populateItem(final Item<Client> item) {
				final Client client = item.getModelObject();
				// add data for table cell in row
				item.add(new Label("login", client.getInfoUser().getLogin()));
				item.add(new Label("password", client.getInfoUser()
						.getPassword()));
				item.add(new Label("name", client.getInfoUser().getName()));
				item.add(new Label("surname", client.getInfoUser().getSurname()));

				// add button component to open "edit" page
				item.add(new Link("add") {
					@Override
					public void onClick() {
						setResponsePage(new AddCreditCardPage(client));
					}
				});
				
				item.add(new Link("delete") {
					@Override
					public void onClick() {
						clientDao.delete(client.getInfoUser().getLogin());
						setResponsePage(new ListClientsPage(infoUser));
					}
				});
				
				item.add(new Link("unlock") {
					@Override
					public void onClick() {
						setResponsePage(new UnlockCardPage(client, infoUser));
					}
				});
			}
		});
		
		add(new Link("ListClients")
		{

			@Override
			public void onClick() {
				setResponsePage(new ListClientsPage(infoUser));
				
			}
			
		});
		
		add(new Link("exit")
		{

			@Override
			public void onClick() {
				setResponsePage(new AuthorizationPage());
				
			}
			
		});

	}
}
